package com.atguigu.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.oss.service.OssService;
import com.atguigu.oss.utils.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * @author: 孟繁钦
 * @createDate: 2021-12-09 20:07
 * @description:
 */
@Service
public class OssServiceImpl implements OssService {

    //文件上传
    @Override
    public String uploadFileAvatar(MultipartFile file) {

        String endPoint = ConstantPropertiesUtils.END_POINT;
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        String bucketName = ConstantPropertiesUtils.BUCKET_NAME;

        try {

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

            // 上传文件流。
            InputStream inputStream = file.getInputStream();

            //获取文件名称
            String fileOriginalFilename = file.getOriginalFilename();

            //使用uuid在文件名称里面添加唯一值 防止文件名重复

            String uuid = UUID.randomUUID().toString().replace("-", "");

            fileOriginalFilename = uuid + fileOriginalFilename;

            String datePath = new DateTime().toString("yyyy/MM/dd");
            //根据日期建立文件分类
            fileOriginalFilename = datePath + "/" + fileOriginalFilename;

            /**
             * 调用oss进行上传
             * bucketName BucketName
             * fileOriginalFilename 上传到oss文件的路径和名称
             * inputStream 文件输入流
             */

            ossClient.putObject(bucketName, fileOriginalFilename, inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();
            //https://meng-edu1010.oss-cn-beijing.aliyuncs.com/3.jpg
            String url = "https://" + bucketName + "." + endPoint + "/" + fileOriginalFilename;
            return url;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}